#include "sobjc/runtime.h"
#include "sobjc/class.h"
#include "sobjc/object.h"
#include "ivar.h"

Class
objc_object_get_class(id obj)
{
  return obj->isa;
}

Ivar
objc_object_get_ivar(id obj, const char *name, void **value)
{
  Ivar ivar = NULL;

  ivar = objc_class_get_ivar(obj->isa, name);
  if (ivar != NULL && value != NULL)
    *value = objc_object_get_ivar_value(obj, ivar);
  return ivar;
}

void *
objc_object_get_ivar_value(id obj, Ivar ivar)
{
  return (void *)((ptrdiff_t)obj + ivar->offset);
}

Ivar
objc_object_set_ivar(id obj, const char *name, void *value)
{
  Ivar ivar = NULL;

  ivar = objc_class_get_ivar(obj->isa, name);
  if (ivar != NULL)
    objc_object_set_ivar_value(obj, ivar, value);
  return ivar;
}

void
objc_object_set_ivar_value(id obj, Ivar ivar, void *value)
{
  void *slot;

  slot = (void *)((size_t)obj + ivar->offset);
  memcpy(slot, value, objc_ivar_get_size(ivar));
}

